<?php

namespace app\admin\model;

use think\Db;
use think\Model;
class ProfitCount extends BaseModel{




    public function set_profit($price,$type="add"){

        $list = Db::name("ProfitCount")->select();

        $time = time();
        if( $type == "add" ){
            $Arr[] = $this->_get_data($list,"total_profit",$price,$time);
            $Arr[] = $this->_get_data($list,"today_profit",$price,$time);
        }else{
            $Arr[] = $this->_get_data($list,"total_defray",$price,$time);
            $Arr[] = $this->_get_data($list,"today_defray",$price,$time);
        }
        $this->saveAll($Arr);
    }

    private function _get_data($list,$key,$price,$time){

        $Arr = [];
        foreach($list as $k=>$v){
            if( $v["key"] == $key){
                $Arr = $v;
                break;
            }
        }
        if( empty($Arr) ){
            $Arr = [
                $key => $key,
                "value" => $price,
                "c_time" => $time
            ];
        }else{
            if( $key == "today_profit" || $key =="today_defray" ){
                $today = strtotime(date("Y-m-d"),$time);
                if( $Arr["c_time"] < $today ){
                    $Arr["value"] = $price;
                }else{
                    $Arr["value"] = $Arr["value"] + $price;
                }
                $Arr["c_time"] = $time;
            }else{
                $Arr["value"] = $Arr["value"] + $price;
            }
        }
        return $Arr;
    }







}
?>